######################################################
##### This file replicates Figure 3 in the paper #####
######################################################
rm(list = ls())
library(dplyr)
library(lubridate)
library(tsibble)

### load violence data

violence <- read.csv("../datasets/0_merged_violence.csv", stringsAsFactors = FALSE)
ISISviol <- violence[violence$group == "Islamic State",]
ISISviol$date <- as.Date(ISISviol$date)
ISISviol <- ISISviol[ISISviol$date >= as.Date("2015-01-01") & ISISviol$date <= as.Date("2020-01-07"),]

ISISviol$yearweek <- yearweek(ISISviol$date)
ISIS <- data.frame()
for (i in 1:length(unique(ISISviol$yearweek))){
  temp <- ISISviol[ISISviol$yearweek == unique(ISISviol$yearweek)[i],]
  yearweek <- unique(ISISviol$yearweek)[i]
  totalevent <- nrow(temp)
  statebased <- sum(temp$attack_type == 1, na.rm = TRUE)
  totaldeaths <- sum(temp$deaths, na.rm = TRUE)
  
  ISIS <- rbind(ISIS, 
                data.frame(as.character(yearweek), totalevent, statebased, totaldeaths))
}
ISIS$yearweek <- yearweek(ISIS$as.character.yearweek.)
ISIS <- ISIS[order(ISIS$yearweek),]


### load ISIS territorial data

territory <- read.csv("../datasets/1_ISISterritory.csv")
territory$date <- as.Date(territory$date)
territory$yearweek <- yearweek(territory$date)

territory2 <- territory %>% 
  group_by(yearweek) %>%
  summarize(territory = mean(territory, na.rm = TRUE))
ISIS <- merge(territory2, ISIS, by = "yearweek", all.x = TRUE)
ISIS$totalevent[is.na(ISIS$totalevent)] <- 0
ISIS$statebased[is.na(ISIS$statebased)] <- 0
ISIS$totaldeaths[is.na(ISIS$totaldeaths)] <- 0
ISIS$week <- as.numeric(ISIS$yearweek)

pdf("../outputplots/figure3.pdf", width = 10, height = 5.5)
par(mfrow = c(2, 1), mar = c(0.5, 4, 1, .7))
plot(NULL,
     ylim = c(0, 200),
     xlim = c(min(ISIS$week), max(ISIS$week)),
     ylab = "Count",
     xlab = NA,
     cex.lab = 1,
     axes = F)
axis(side = 2, at = seq(0, 200, by = 50))
points(x = ISIS$week, y=ISIS$totaldeaths/10, pch = 18, col = "lightcoral", cex=1)
points(x = ISIS$week, y=ISIS$statebased, pch = 17, col = "orange", cex=0.9)
points(x = ISIS$week, y=ISIS$totalevent, pch = 19, col = "purple", cex=0.9)
legend(ISIS$week[ISIS$yearweek == yearweek("2019 W01")], 150,
       legend = c("Total Violent Events", "State-based Battles", "Total Deaths/10"),
       col = c("purple", "orange", "lightcoral"), pch = c(19, 17, 18))

par(mar = c(5, 4, 0.5, .7))
plot(NULL,
     ylim = c(0, 27),
     xlim = c(min(as.numeric(ISIS$yearweek)), max(as.numeric(ISIS$yearweek))),
     ylab = "Area",
     xlab = "Weeks: from 2015-01-01 to 2019-12-31",
     cex.lab = 1,
     axes = F)
axis(side = 2, at = seq(0, 26, by = 5))
points(x=as.numeric(ISIS$yearweek), y=ISIS$territory, pch = 15, col = "forestgreen", cex=0.9)
axis(side = 1, at = c(ISIS$week[ISIS$yearweek %in% c(yearweek("2015 W01"), yearweek("2016 W01"), yearweek("2017 W01"), yearweek("2018 W01"), yearweek("2019 W01"), yearweek("2020 W01"))]),
     labels = c("2015", "2016", "2017", "2018", "2019", "2020"),
     cex.axis = 1)
legend(ISIS$week[ISIS$yearweek == yearweek("2019 W01")], 15,
       legend = c("Territory"),
       col = c("forestgreen"), pch = 15)
dev.off()

